﻿using Game.NetWork;
using GameEntitySystem;

namespace Game
{
    public class ComponentHealPackage : IPackage
    {
        public enum EventType
        {
            SyncStat,
            StartHeal,
            Stop
        }

        public float m_fluDuration;

        public float rate;

        public float dt1;

        public EventType m_eventType;

        public ushort entityId;

        public byte ID => 106;

        public Client To { get; set; }

        public Client Except { get; set; }

        public Client From { get; set; }

        public ClientState MinNeedState => ClientState.ProjectLoaded;

        public ComponentHealPackage()
        {
        }

        public ComponentHealPackage(ComponentHealBuffA flu)
        {
            entityId = flu.Entity.EntityId;
            rate = flu.m_HealRate;
            m_fluDuration = flu.m_HealDuration;
        }

        public void WriteData(PackageStreamWriter writer)
        {
            writer.Write(entityId);
            writer.Write(m_fluDuration);
            writer.Write(rate);
        }

        public void ReadData(PackageStreamReader reader)
        {
            entityId = reader.ReadUInt16();
            m_fluDuration = reader.ReadSingle();
            rate = reader.ReadSingle();
        }

        public void Handle(ProjectNet projectNet, NetNode netNode, bool isServer)
        {
            projectNet.FindEntityById(entityId, delegate (Entity entity)
            {
                ComponentHealBuffA componentHealBuffA = entity.FindComponent<ComponentHealBuffA>();
                if (componentHealBuffA != null)
                {
                    componentHealBuffA.m_HealDuration = m_fluDuration;
                    if (m_fluDuration > 0f)
                    {
                        componentHealBuffA.healing();
                    }
                }
            });
        }
    }
}